import XTestRunner, HTMLTestRunner_PY3, BeautifulReport
from datetime import datetime
import time, logging, yagmail, glob


class Common():

    def Log(self):
        timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
        log_filename = rf'D:\zhuangaoliu\UI_zdh_zg6_lx\Log\{timestamp}.log'
        logging.basicConfig(
            level=logging.DEBUG,
            format='%(levelname)s -> %(asctime)s -> %(message)s -> %(filename)s -> %(lineno)d',
            filename=log_filename,
            filemode='a+'
            # handlers=[
            #     logging.FileHandler(log_filename, mode='a+'),
            #     logging.StreamHandler()
            # ]
        )
        return logging.getLogger(__name__)


    def Report1(self):
        timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
        html_filename = rf'D:\zhuangaoliu\UI_zdh_zg6_lx\Report\{timestamp}.html'
        report = XTestRunner.HTMLTestRunner(open(html_filename, 'wb'), title='UI项目测试报告',
                                            description='测试报告')
        return report

    def Report2(self):
        timestamp = time.strftime('%Y%m%d%H%M%S', time.localtime())
        html_filename = rf'D:\zhuangaoliu\UI_zdh_zg6_lx\Report\{timestamp}.html'
        report = HTMLTestRunner_PY3.HTMLTestRunner(open(html_filename, 'wb'), title='py3报告——webUItest', description='webUI测试报告')
        return report

    def Report3(self, suite):

        timestamp = time.strftime('%Y%m%d%H%M%S', time.localtime())
        html_filename = rf'D:\zhuangaoliu\UI_zdh_zg6_lx\Public\{timestamp}.html'
        report = BeautifulReport.BeautifulReport(suites=suite).report(filename=html_filename, description='Beautiful报告', log_path='.', report_dir='.')

    def mail(self):
        mail = yagmail.SMTP(user='2318636512@qq.com', password='lmakcmojpqpjdhhj', host='smtp.qq.com', port=465)
        attachments = glob.glob(rf'D:\zhuangaoliu\UI_zdh_zg6_lx\Log\*log') + glob.glob(rf'D:\zhuangaoliu\UI_zdh_zg6_lx\Report\*html')
        mail.send(to='2318636512@qq.com', subject='测试报告与日志文件', contents='请注意查收', attachments=attachments)
        mail.close()













